package xjh.him.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xjh.him.R;

import java.net.InetSocketAddress;

import xjh.him.common.BgTaskActivity;
import xjh.him.common.ErrorMsg;
import xjh.him.common.SharedData;
import xjh.him.netty.NettyClient;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.UserDto;
import xjh.him.util.ToastUtil;

public class RegisterActivity extends BgTaskActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        loginUrlOnClick();
        register();
    }

    public void loginUrlOnClick() {
        TextView loginUrl = findViewById(R.id.login_url);
        loginUrl.setOnClickListener((view) -> {
            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(loginIntent);
            finish();
        });
    }

    public void register() {
        Button btn = findViewById(R.id.register);
        btn.setOnClickListener((view) -> {
            EditText phoneText = findViewById(R.id.register_phone);
            EditText nicknameText = findViewById(R.id.register_nickname);
            EditText pwdText = findViewById(R.id.register_pwd);
            EditText pwdConfirmText = findViewById(R.id.register_pwd_confirm);

            if (!pwdText.getText().toString().equals(pwdConfirmText.getText().toString())) {
                ToastUtil.show(this, "密码不一致，请重新输入");
            }else {
                NettyClient client = SharedData.client;
                if (client == null) {
                    client = new NettyClient();
                    SharedData.client = client;
                    InetSocketAddress address = new InetSocketAddress(
                            getString(R.string.netty_server_address),
                            Integer.parseInt(getString(R.string.netty_server_port)));
                    client.connect(address);
                }

                UserDto.RegisterReq req = new UserDto.RegisterReq();
                req.setPhone(phoneText.getText().toString());
                req.setNickname(nicknameText.getText().toString());
                req.setPassword(pwdText.getText().toString());
                client.sendMsg(Packet.create(UserDto.RegisterReq.class, req));
                Object obj = SharedData.future.get();
                if (obj instanceof UserDto.RegisterResp) {
                    UserDto.RegisterResp resp = (UserDto.RegisterResp) obj;
                    SharedData.me = resp.getUser();
                    SharedData.token = resp.getToken();
                }else if (obj instanceof ErrorMsg) {
                    ToastUtil.show(this, ((ErrorMsg)obj).getMsg());
                    return;
                }

                Intent homeIntent = new Intent(RegisterActivity.this, HomeActivity.class);
                startActivity(homeIntent);
                finish();
            }
        });
    }
}
